import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ResourceBundle;

import org.weso.lodin.appserv.TransformerAppServ;
import org.weso.lodin.datasource.config.DatasetDirTemplateDAOImpl;
import org.weso.lodin.datasource.config.DatasetListing;
import org.weso.lodin.datasource.config.DatasetTemplateDAO;
import org.weso.lodin.datasource.config.DatasetTemplateDAOPrimaryImpl;
import org.weso.lodin.generated.Dataset;
import org.weso.lodin.generated.TypeTable;
import org.weso.lodin.generated.TypeValues;
import org.weso.lodin.utils.PrefixManager;
import org.weso.lodin.utils.TransformerHelper;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;


public class GenerateDatasets {

	public static void main(String []args) throws FileNotFoundException, IOException{
		ResourceBundle mapPrefixesURIs = PrefixManager.getResourceBundle();		
		Model model = ModelFactory.createDefaultModel();
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		DatasetTemplateDAO daoPrimary = new DatasetTemplateDAOPrimaryImpl(
				contextClassLoader.getResourceAsStream(TransformerAppServ.WEBINDEX_RAW_DATA_XLS));
		String dir =  DatasetListing.defaultPathData;
		DatasetTemplateDAO daoSecondary = new DatasetDirTemplateDAOImpl(dir);
		Resource qbDataset = model.createResource(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"dataset");
		Property qbStructureProp = model.createProperty(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"structure");
		Property qbSliceProp = model.createProperty(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"slice");
		Property qbSliceKey = model.createProperty(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"sliceKey");
		Resource qbDsd= model.createResource(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"DataSetDefinition");
		Property qbComponent= model.createProperty(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"component");
		Property qbDimension= model.createProperty(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"dimension");
		Property qbOrder= model.createProperty(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"order");
		Property qbMeasure= model.createProperty(TransformerAppServ.HTTP_PURL_ORG_LINKED_DATA_CUBE+"measure");
		Resource sliceByCountry = model.createResource(TransformerAppServ.INDICATOR_BASE_URI+"sliceByCountry");
		Resource refArea = model.createResource(TransformerAppServ.ONTOLOGY_BASE_URI+"ref-area");
		Resource refIndicator = model.createResource(TransformerAppServ.ONTOLOGY_BASE_URI+"ref-indicator");
		//FIXME: Refactor
		for(Dataset dataset:daoPrimary.getDatasets()){
			//Create DSD
			createDSD(model, qbSliceKey, qbDsd, qbComponent, qbDimension,
					qbOrder, qbMeasure, sliceByCountry, refArea, refIndicator,
					dataset.getId());
//			//Create Dataset
//			Resource ds = model.createResource(TransformerAppServ.DATASET_BASE_URI+dataset.getId());			
//			model.add(ds,RDF.type,qbDataset);			
//			Resource qbStructure = model.createResource(TransformerAppServ.DSD_BASE_URI+dataset.getId());
//			model.add(ds,qbStructureProp,qbStructure);
//			for(TypeTable table:dataset.getTable()){				
//				for(TypeValues value:table.getValues()){
//					Resource qbSlice= model.createResource(TransformerAppServ.SLICES_BASE_URI+dataset.getId()+"/"+value.getYear().getYear());
//					model.add(ds,qbSliceProp,qbSlice);	
//				}
//			}
		}
		
		
		for(Dataset dataset:daoSecondary.getDatasets()){
			//Create DSD
			createDSD(model, qbSliceKey, qbDsd, qbComponent, qbDimension,
					qbOrder, qbMeasure, sliceByCountry, refArea, refIndicator,
					dataset.getId());
//			//Create Dataset
//			Resource ds = model.createResource(TransformerAppServ.DATASET_BASE_URI+dataset.getId());			
//			model.add(ds,RDF.type,qbDataset);			
//			Resource qbStructure = model.createResource(TransformerAppServ.DSD_BASE_URI+dataset.getId());
//			model.add(ds,qbStructureProp,qbStructure);
//			for(TypeTable table:dataset.getTable()){				
//				for(TypeValues value:table.getValues()){
//					Resource qbSlice= model.createResource(TransformerAppServ.SLICES_BASE_URI+dataset.getId()+"/"+value.getYear().getYear());
//					model.add(ds,qbSliceProp,qbSlice);	
//				}
//			}
		}
		TransformerHelper.serializeModel(model,mapPrefixesURIs,IndicatorsAppServMain.DIR_GENERATED+File.separator+"datasets-complete.ttl");	
	}

	private static void createDSD(Model model, Property qbSliceKey,
			Resource qbDsd, Property qbComponent, Property qbDimension,
			Property qbOrder, Property qbMeasure, Resource sliceByCountry,
			Resource refArea, Resource refIndicator, String dataset) {
		Resource dsd = model.createResource(TransformerAppServ.DSD_BASE_URI+dataset);
		model.add(dsd,RDF.type,qbDsd);
		//model.add(dsd,qbSliceKey,sliceByCountry);
		Resource blankResource = model.createResource();
		blankResource.addProperty(qbDimension, refArea);
		blankResource.addLiteral(qbOrder, 1);
		Resource blankResource1 = model.createResource();
		blankResource1.addProperty(qbDimension, refArea);
		blankResource1.addLiteral(qbOrder, 2);
		Resource blankResource2 = model.createResource();
		blankResource2.addProperty(qbMeasure, refIndicator);		
		dsd.addProperty(qbComponent, blankResource);
		dsd.addProperty(qbComponent, blankResource1);
		dsd.addProperty(qbComponent, blankResource2);
	}
}
